package cn.edu.sdjzu.xg23.kcsj01_backend.controller;


import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Project;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.ProjectService;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/xmgl")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @PostMapping("/project")
    @PreAuthorize("hasAuthority('project:add')")
    public Result add(@RequestBody Project project) {
        projectService.save(project);
        return Result.success("新增成功");
    }

    @DeleteMapping("/project/{id}")
    @PreAuthorize("hasAuthority('project:delete')")
    public Result delete(@PathVariable Long id) {
        projectService.deleteById(id);
        return Result.success("删除成功");
    }

    @PutMapping("/project")
    @PreAuthorize("hasAuthority('project:update')")
    public Result update(@RequestBody Project project) {
        projectService.update(project);
        return Result.success("修改成功");
    }

    // 修改后的 ProjectController.java 第37行开始的代码
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('project:search:all') or hasAuthority('project:search:my')")
    public Result selectProjectsByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) Project project,
            @RequestParam(required = false) Long employeeId) {
        PageInfo<Project> pageInfo = projectService.selectProjectsByCondition(pageNum, pageSize, project,employeeId);
        return Result.success(pageInfo);
    }
    @PreAuthorize("hasAuthority('project:approve')")
    @PutMapping("/approve")
    public Result approveProject(@RequestBody Project project) {
        if (projectService.approveProject(project)) {
            return Result.success("审批成功");
        } else {
            return Result.error("500", "审批失败");
        }
    }

}
